﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using Core.Base;
using ERP.BusinessEntities;

namespace MainApp.Modules.InvAdj
{
    public class InvAdjModule: BaseModule
    {
        public InvAdjModule()
        {
            this.Name = "InvAdj";
            entity = new InvAdjEntity();
            Init();
        }

        #region calculate
        public override void CalcItem_FK_ICProducts_ID(Object objItem, ICProductsInfo Product)
        {
            AppContext.SetProperty(objItem, AppContext.cstFKStock, Product.FK_ICStocks_ID);
            AppContext.SetProperty(objItem, AppContext.cstFKStockUOM, Product.FK_ICStockUOMs_ID);
            AppContext.SetProperty(objItem, AppContext.cstFKTaxCode, Product.FK_TxTaxCodes_OutVATID);
            AppContext.SetProperty(objItem, AppContext.cstFUnitPrice, Product.SaleUnitPrice);
        }
        #endregion

        #region Validating
        public override string ObjectValidating(object obj)
        {
            StringBuilder strBuilder = new StringBuilder();
            if (obj == null) return String.Empty;

            if (obj is ICShipmentItemsInfo)
            {
                ICShipmentItemsInfo ShipmentItem = (ICShipmentItemsInfo)obj;
                if (ShipmentItem.FK_ICProducts_ID == 0)
                    strBuilder.Append("Vui lòng chọn sản phẩm\n");
                if (ShipmentItem.FK_ICStocks_ID == 0)
                    strBuilder.Append("Vui lòng chọn Kho\n");
            }
            else if (obj is ICReceiptItemsInfo)
            {
                ICReceiptItemsInfo ReceiptItem = (ICReceiptItemsInfo)obj;
                if (ReceiptItem.FK_ICProducts_ID == 0)
                    strBuilder.Append("Vui lòng chọn sản phẩm\n");
                if (ReceiptItem.FK_ICStocks_ID == 0)
                    strBuilder.Append("Vui lòng chọn Kho\n");
            }

            return strBuilder.ToString();
        }

        public override string FieldValidating(object obj, String strFieldName, Object objNewValue)
        {
            String strErrorText = String.Empty;
            if (obj == null) return strErrorText;

            if ((obj is ICShipmentItemsInfo || obj is ICReceiptItemsInfo) && strFieldName == "FQty")
            {
                int iFQty = Convert.ToInt32(objNewValue);
                if (iFQty <= 0)
                    strErrorText = "Số lượng phải lớn hơn 0";
            }

            return strErrorText;
        }
        #endregion

    }
}
